package com.htfyun.eink.pw.draw.pen.epd.charcoal;

public class ImageUtils {

    //sierraLiteRgb32 for bitmapData
    public static void sierraLiteRgb32(BitmapData bitmapData) {
        int[][] rgb = new int[bitmapData.width][bitmapData.height];
        int width = bitmapData.width;
        int height = bitmapData.height;

        if (width >= 1) {
            for (int row = 0; row < bitmapData.width; row++) {
                for (int column = 0; column < bitmapData.height; column++) {
                    rgb[row][column] = ColorUtils.grayscaleFromRgb32(bitmapData.data[row][column]);
                }
            }
        }

        if (height >= 1) {
            int val;

            for (int row = 0; row < bitmapData.width; row++) {
                for (int column = 0; column < bitmapData.height; column++) {
                    int alpha = rgb[row][column] & 0xFF;
                    int grayscale = (alpha << 0x18 >> 0x1F) & 0xFF;
                    val = ColorUtils.grayscaleToRgb32(grayscale);
                    bitmapData.data[row][column] = val;

                    double diff = alpha - grayscale;

                    if (column + 1 < bitmapData.height) {
                        int v24 = (rgb[row][column + 1] & 0xFF);
                        val = ColorUtils.clampInGrayscale((int) (diff * 0.5F + (double) v24));
                        rgb[row][column + 1] = val;
                    }
                    if (column > 0 && row + 1 < bitmapData.width) {
                        int v24 = (rgb[row + 1][column - 1] & 0xFF);
                        val = ColorUtils.clampInGrayscale((int) (diff * 0.25F + (double) v24));
                        rgb[row + 1][column - 1] = val;
                    }
                    if (row + 1 < bitmapData.width) {
                        int v24 = (rgb[row + 1][column] & 0xFF);
                        val = ColorUtils.clampInGrayscale((int) (diff * 0.25F + (double) v24));
                        rgb[row + 1][column] = val;
                    }
                }
            }
        }

    }
}
